import {Inject, Injectable} from '@nestjs/common';
import {Repository} from "typeorm";
import {Supplier} from "@/entitys/supplier/supplier.entity";
import {BaseService} from "@/modules/_/base/base.service";
import {InjectRepository} from "@nestjs/typeorm";
import {PageProps} from "@/typing/paging/paging";
import {SupplierType} from "@/typing/supplierType";
import {CommonUtils} from "@/modules/common/common.utils";
import {ResultUtils} from "@/utils/resultUtils";

@Injectable()
export class SupplierService extends BaseService {

    constructor(
        @InjectRepository(Supplier) readonly Repository: Repository<Supplier>,
        private readonly commonUtils: CommonUtils
    ) {
        super(Repository);
    }

    async options(pageProps: PageProps & {supplierType: SupplierType}) {
        const {pageSize, supplierType, page} = pageProps;
        const user = this.commonUtils.getUser();

        // const pagination = this.commonUtils.getPagination(pageProps);

        const total = await this.repository.count();

        const options = await this.repository.find({
            where: {supplierType, companyId: user.companyId},
            take: pageSize,
        });

        if (user) {
            return ResultUtils.success({
                data: options,
                total,
                page,
                limit: pageSize,
            });
        }
        return ResultUtils.error(null, "err", "");
    }
}
